package com.haredot.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import com.haredot.entity.Advert;
import com.haredot.oss.properties.OssProperties;
import com.haredot.service.AdvertService;
import com.haredot.validation.Insert;
import com.haredot.validation.Update;
import com.haredot.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.lang.Nullable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

@RestController
@RequestMapping("/advert")
@EnableConfigurationProperties(OssProperties.class)
public class AdvertController {

    private AdvertService advertService;

    private OssProperties ossProperties;

    private OSS ossClient;

    @Autowired
    public AdvertController(AdvertService advertService, @Nullable OSS ossClient, OssProperties ossProperties) {
        this.advertService = advertService ;
        this.ossClient = ossClient ;
        this.ossProperties = ossProperties ;
    }


    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('advert')")
    public R<?> list() {
        return advertService.queryAverts();
    }

    @PostMapping("/upload")
    @PreAuthorize("hasAnyAuthority('advert:add', 'advert:modify')")
    public R<?> upload(@RequestPart("swiper") MultipartFile part) throws Exception {
        // 获取 要存储在 磁盘上的文件名
        String filename = "advert/" + UUID.randomUUID() + part.getOriginalFilename();
        // 构建 PutObjectRequest对象
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossProperties.getBucketName(), filename, part.getInputStream());
        // 上传文件到网盘
        ossClient.putObject(putObjectRequest);
        // 返回结果
        return R.ok().data(Map.of("url", filename));
    }


    @PostMapping("/add")
    @PreAuthorize("hasAuthority('advert:add')")
    public R<?> addAdvert(@RequestBody @Validated(Insert.class) Advert advert) {
        return advertService.saveAdvert(advert);
    }

    @PutMapping("/update/{id}")
    @PreAuthorize("hasAuthority('advert:modify')")
    public R<?> updateAdvert(@PathVariable("id") Long id ,@RequestBody @Validated(Update.class) Advert advert) {
        Assert.isTrue(Objects.equals(id, advert.getId()), "ID参数不正确");
        return advertService.updateAdvert(advert);
    }


    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('advert:remove')")
    public R<?> removeAdvert(@PathVariable("id") Long id) {
        return advertService.removeById(id);
    }

    @DeleteMapping()
    @PreAuthorize("hasAuthority('advert:batch:remove')")
    public R<?> batchRemoveAdvert(@RequestBody List<Long> ids) {
        return advertService.removeByIds(ids);
    }


}
